package com.isesol.ime.validator.subscription;

import com.isesol.arch.fluentvalidator.*;
import com.isesol.ime.dal.mapper.*;
import com.isesol.ime.dal.model.dto.*;
import org.apache.commons.lang3.*;
import org.springframework.stereotype.*;

import javax.annotation.*;

@Component
public class SubscriptionCreateMsgExistValidator extends ValidatorHandler<SubscriptionDTO> implements Validator<SubscriptionDTO> {

	@Resource
	MessageMapper messageMapper;

	@Override
	public boolean validate(ValidatorContext context, SubscriptionDTO subscriptionDTO) {

		boolean result = true;

		if (StringUtils.isNotEmpty(subscriptionDTO.getMsgIds())){

			String[] msgId = subscriptionDTO.getMsgIds().split(",");

			for (int i = 0; i < msgId.length; i++) {

				if (null == messageMapper.getById(Long.parseLong(msgId[i]))) {

					context.addErrorMsg("params error,message not exist,id = "+msgId[i]);
					result= false;

				}
			}
		}

		return result;
	}
}